package com.sigmazero13.bgjeeves.model;

import java.util.ArrayList;
import java.util.List;

import android.os.Parcel;
import android.os.Parcelable;

public class GamePlayerInfo implements Parcelable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5951009040874449691L;
	private String name;
	private String username;
	private List<String> teams;
	private Integer startingPos;
	private Integer score;
	private Boolean winner;	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public List<String> getTeams() {
		return teams;
	}
	public void setTeams(List<String> teams) {
		this.teams = teams;
	}
	public void addTeam(String team) {
		if (teams == null) {
			teams = new ArrayList<String>();
		}
		teams.add(team);
	}
	public Integer getStartingPos() {
		return startingPos;
	}
	public void setStartingPos(Integer startingPos) {
		this.startingPos = startingPos;
	}
	public Integer getScore() {
		return score;
	}
	public void setScore(Integer score) {
		this.score = score;
	}
	public Boolean getWinner() {
		return winner;
	}
	public void setWinner(Boolean winner) {
		this.winner = winner;
	}
	
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("PLAYER INFO {\n");
		sb.append("Name: " + getName() + "\n");
		sb.append("Username: " + getUsername() + "\n");
		sb.append("Team: " + getCombinedTeams() + "\n");
		sb.append("Starting Position: " + getStartingPos() + "\n");
		sb.append("Score: " + getScore() + "\n");
		sb.append("Winner? " + (getWinner()?"yes":"no") + "\n");
		sb.append("}");
		
		return sb.toString();
	}
	
	public String getCombinedTeams() {
		if (teams == null) return "";
		StringBuilder teamString = new StringBuilder();
		for (String team : teams) {
			teamString.append(team + ", ");
		}
		teamString.setLength(teamString.length()-2);
		
		return teamString.toString();
	}
	
	@Override
	public int describeContents() {
		return 0;
	}
	
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		int tempStartingPos = (startingPos == null) ? -1 : startingPos;
		int tempScore = (score == null) ? 0 : score;
		
		dest.writeString(name);
		dest.writeString(username);
		dest.writeStringList(teams);
		dest.writeInt(tempStartingPos);
		dest.writeInt(tempScore);
		dest.writeInt(winner?1:0);
	}
	
	public static final Parcelable.Creator<GamePlayerInfo> CREATOR = new Parcelable.Creator<GamePlayerInfo>() {
		@Override
		public GamePlayerInfo createFromParcel(Parcel in) {
			GamePlayerInfo player = new GamePlayerInfo();
			player.setName(in.readString());
			player.setUsername(in.readString());
			List<String> teams = new ArrayList<String>();
			in.readStringList(teams);
			player.setTeams(teams);
			player.setStartingPos(in.readInt());
			player.setScore(in.readInt());
			player.setWinner(in.readInt()==1);
			
			return player;
		}
		
		@Override
		public GamePlayerInfo[] newArray(int size) {
			return new GamePlayerInfo[size];
		}
	};
}
